//
//  MainViewController.m
//  TaurusXi
//
//  Created by xicheng on 15/11/27.
//  Copyright © 2015年 xicheng. All rights reserved.
//

#import "MainViewController.h"
#import "RegisterViewController.h"
#import "HomeViewController.h"
#import "AppDelegate.h"
#import "LoginViewController.h"
#import "UserHelper.h"
#import "WebViewController.h"
@interface MainViewController ()

@property(nonatomic,strong)UILabel *memberLevelLabel;
@property(nonatomic,strong)UILabel *phoneNumb;
@property(nonatomic,strong)UILabel *totalMoneyLabel;
@property(nonatomic,strong)UILabel *levelLabel;

@property(nonatomic,strong)UIImageView *avatarImageView;
@property(nonatomic,strong)UIImageView *phoneBgImageView;
@property(nonatomic,strong)UIButton *loginBt;
@property(nonatomic,strong)UIButton *registerBt;

@end

@implementation MainViewController

- (void)viewDidLoad {
  [super viewDidLoad];
  [self initView];
  [self loadData];
  [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(loginStatus:) name:@"login" object:nil];
}


-(void)loadData{
  
  Boolean isLoginFlag = [[UserHelper shareUserInstance] isLogin];
  [self showLoginStatus:isLoginFlag];
  if (!isLoginFlag) {
    return;
  }

  [self showSimplePro];
  @weakify(self);
  [[Api shareApiInstance]getIndexDataWithResponce:^(id object) {
    @strongify(self);
    
    [self hideSimpleProWithDelay:0];
    
      NSString *memberLevelStr = [NSString stringWithFormat:@"%@",[object objectForKey:@"memberlevel"]];
      self.memberLevelLabel.text = memberLevelStr;
      self.totalMoneyLabel.text =[NSString stringWithFormat:@"%@",[object objectForKey:@"totalmoney"]];
       self.phoneNumb.text = [NSString stringWithFormat:@"%@",[object objectForKey:@"username"]];
      self.levelLabel.text =[NSString stringWithFormat:@"%@:%@/%@",memberLevelStr,[object objectForKey:@"levelvalue"],[object objectForKey:@"levelmax"]];

      [self.memberLevelLabel sizeToFit];
      [self.totalMoneyLabel sizeToFit];
      [self.phoneNumb sizeToFit];
      [self.levelLabel sizeToFit];
      [self showLoginStatus:YES];
  } AndError:^(id object, NSError *error) {
     @strongify(self);
    [self hideSimpleProWithDelay:0];
  }];
  
}


-(void)showLoginStatus:(Boolean)isLoginFlag{

  
    self.loginBt.hidden = isLoginFlag?YES:NO;
    self.registerBt.hidden = isLoginFlag?YES:NO;
  self.phoneBgImageView.hidden = isLoginFlag?NO:YES;
  self.phoneNumb.hidden = isLoginFlag?NO:YES;
  self.avatarImageView.hidden = isLoginFlag?NO:YES;
  self.memberLevelLabel.hidden = isLoginFlag?NO:YES;

}

-(void) initView{

  self.navigationController.navigationBarHidden = NO;
  self.view.clipsToBounds= YES;
  [self initBgAndTitle];
    self.totalMoneyLabel = [[UILabel alloc] init];
    self.totalMoneyLabel.text = @"0.00";
//    phoneBigLabel.textColor = [UIColor whiteColor];
    self.totalMoneyLabel.font = [UIFont systemFontOfSize:18];
    [self.totalMoneyLabel sizeToFit];
    self.totalMoneyLabel.textColor = [UIColor colorWithRed:255/255. green:254/255. blue:179/255. alpha:1];
    self.totalMoneyLabel.frame = CGRectMake(190, 46+44, self.totalMoneyLabel.frame.size.width, self.totalMoneyLabel.frame.size.height);
    [self.view addSubview:self.totalMoneyLabel];
  
  
  //
  CGRect chongzhiFrame =  CGRectMake(self.view.frame.size.width-75, 44+44, 55, 25);
  NSLog(@"Button_frame:%@",NSStringFromCGRect(chongzhiFrame));
  UIButton *chongzhiBt = [[UIButton alloc]initWithFrame:chongzhiFrame];
  [chongzhiBt setImage:(ImageWithContentsOfFileWithPng(@"ic_the-home-page_recharge@2x")) forState:UIControlStateNormal ];
//   [chongzhiBt setImage:ImageWithContentsOfFileWithPng(@"ic_the-home-page_recharge@2x") forState:UIControlStateHighlighted ];
//  [chongzhiBt setAdjustsImageWhenHighlighted:NO];
  [self.view addSubview:chongzhiBt];
  
  [self initFiveButton];
  
  
}


-(void)loginStatus:(Boolean)status{
  if (status) {
    [self loadData];
  }
}


-(void)initBgAndTitle{

  UIImageView *bgImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, kStatusBarHeight,self.view.frame.size.width,self.deviceHeight-kStatusBarHeight -kTabBarHeight)];
  bgImageView.image = ImageWithContentsOfFileWithPng(@"bg_beijing_@2x");
  bgImageView.contentMode = UIViewContentModeScaleAspectFill;
  self.customNavigationBar.hidden = YES;
  [self.view addSubview:bgImageView];
  
 
  const CGFloat kTitleHeight = 44 ;
  
  UIImageView *imageTitleButtom = [[UIImageView alloc]initWithFrame:CGRectMake(0, kStatusBarHeight+kTitleHeight+ 10, self.deviceWidth, 106/2)];
  imageTitleButtom.image =ImageWithContentsOfFileWithPng(@"bg_title_bottom@2x");
  [self.view addSubview:imageTitleButtom];
  
  UIImageView *imageTitleLeft = [[UIImageView alloc]initWithFrame:CGRectMake(0, kStatusBarHeight+kTitleHeight, 314/2, 125/2)];
  imageTitleLeft.image =ImageWithContentsOfFileWithPng(@"bg_title_left@2x");
  [self.view addSubview:imageTitleLeft];
  
  UIImageView *imageTitleRMB = [[UIImageView alloc]initWithFrame:CGRectZero];
  imageTitleRMB.image =ImageWithContentsOfFileWithPng(@"ic_object_rmb@2x");
  [self.view addSubview:imageTitleRMB];
  [imageTitleRMB sizeToFit];
  imageTitleRMB.frame = CGRectMake(imageTitleLeft.frame.size.width, kStatusBarHeight+22+kTitleHeight, imageTitleRMB.frame.size.width, imageTitleRMB.frame.size.height);
  
  
  
  //
  
  UIImageView *progressBg = [[UIImageView alloc]initWithFrame:CGRectZero];
  progressBg.backgroundColor = [UIColor grayColor];
  progressBg.image = ImageWithContentsOfFileWithPng(@"bg_jindutiao@2x");
  [progressBg sizeToFit];
  
  UIView *progressView = [[UIView alloc]initWithFrame:CGRectMake(6, imageTitleLeft.frame.origin.y+imageTitleLeft.frame.size.height+10, progressBg.frame.size.width, progressBg.frame.size.height)];
  [progressView setClipsToBounds:YES];
  progressView.backgroundColor = [UIColor clearColor];
  [self.view addSubview:progressView];
  [progressView addSubview:progressBg];
  
  UIImageView *progressImageView = [[UIImageView alloc] initWithFrame:CGRectZero];
  UIImage *progressImage = ImageWithContentsOfFileWithPng(@"bg_hongtiao@2x");
  progressImage = [progressImage resizableImageWithCapInsets:UIEdgeInsetsMake(0, 6, 0, 6) resizingMode:UIImageResizingModeStretch];
  progressImageView.image = progressImage;
  [progressView addSubview:progressImageView];
  CGFloat progress = 0.0;
  progressImageView.frame = CGRectMake(1, 2, (progressView.frame.size.width-2)*progress, progressView.frame.size.height-2);
  
  self.levelLabel = [[UILabel alloc ]init];
  self.levelLabel.text = @"布衣:0/100";
  self.levelLabel.textColor = [UIColor whiteColor];
  self.levelLabel.font = [UIFont systemFontOfSize:10];
  [self.levelLabel sizeToFit];
  [progressView addSubview:self.levelLabel];
  self.levelLabel.center =CGPointMake(progressView.frame.size.width/2, progressView.frame.size.height/2);
  //
  self.avatarImageView = [[UIImageView alloc]initWithFrame:CGRectMake(25, 25+kTitleHeight, 25,25)];
  self.avatarImageView.image = ImageWithContentsOfFileWithPng(@"ic_space_title_photo@2x");
  [self.view addSubview:self.avatarImageView];
  
  //
  self.phoneBgImageView = [[UIImageView alloc]initWithFrame:CGRectMake(5, 50+kTitleHeight, 138,29)];
  self.phoneBgImageView.image = ImageWithContentsOfFileWithPng(@"bg_space_title_name");
  [self.view addSubview:self.phoneBgImageView];
  
  
  self.loginBt = [[UIButton alloc]initWithFrame:CGRectMake(15, 28+kTitleHeight, 30, 47)];
  UIImage *loginImage = ImageWithContentsOfFileWithPng(@"ic_denglu_@2x");
  [self.loginBt setImage:loginImage forState:UIControlStateNormal];
  [self.loginBt setImage:loginImage forState:UIControlStateHighlighted];
  [self.view addSubview:self.loginBt];
  [self.loginBt addTarget:self action:@selector(loginClick) forControlEvents:UIControlEventTouchUpInside];
  
  self.registerBt = [[UIButton alloc]initWithFrame:CGRectMake(self.loginBt.frame.origin.x+self.loginBt.frame.size.width+10 , self.loginBt.frame.origin.y, 30, 47)];
  UIImage *registerImage = ImageWithContentsOfFileWithPng(@"ic_zhuce@2x");
  [self.registerBt setImage:registerImage forState:UIControlStateNormal];
  [self.registerBt setImage:registerImage forState:UIControlStateHighlighted];
  [self.view addSubview:self.registerBt];
  [self.registerBt addTarget:self action:@selector(registerClick) forControlEvents:UIControlEventTouchUpInside];
  
  self.memberLevelLabel = [[UILabel alloc] init];
  self.memberLevelLabel.text = @"布衣";
  self.memberLevelLabel.textColor = [UIColor whiteColor];
  self.memberLevelLabel.font = [UIFont systemFontOfSize:10];
  [self.memberLevelLabel sizeToFit];
  self.memberLevelLabel.frame = CGRectMake(54, 36+kTitleHeight, self.memberLevelLabel.frame.size.width, self.memberLevelLabel.frame.size.height);
  [self.view addSubview:self.memberLevelLabel];
  
  //
  self.phoneNumb = [[UILabel alloc] init];
  self.phoneNumb.text = @"18513175348";
  self.phoneNumb.textColor = [UIColor whiteColor];
  self.phoneNumb.font = [UIFont systemFontOfSize:12];
  [self.phoneNumb sizeToFit];
  self.phoneNumb.frame = CGRectMake(25, 58+kTitleHeight, self.phoneNumb.frame.size.width, self.phoneNumb.frame.size.height);
  [self.view addSubview:self.phoneNumb];

  
  UIView *navigationBar = [[UIView alloc]initWithFrame:CGRectMake(0, kStatusBarHeight, self.deviceWidth, kTitleHeight)];
  navigationBar.backgroundColor = [UIColor colorWithRed:3/255. green:22/255. blue:52/255. alpha:0.77];
  
  [self.view addSubview:navigationBar];
  
  UILabel *titleLable =[[UILabel alloc]init];
  titleLable.text = @"金牛座";
  titleLable.textColor = [UIColor whiteColor];
  titleLable.font = [UIFont systemFontOfSize:18];
  [titleLable sizeToFit];
  [navigationBar addSubview:titleLable];
  titleLable.center = CGPointMake(self.deviceWidth/2, navigationBar.frame.size.height/2);
  
}


-(void)initFiveButton{
  
  UIImageView *centerImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 730/2, 730/2)];
  centerImageView.image =  ImageWithContentsOfFileWithPng(@"bg_xingyu_yuan_new@2x");
  
  [self.view addSubview:centerImageView];
  centerImageView.center = CGPointMake(self.view.center.x, (self.deviceHeight - kStatusBarHeight -kTabBarHeight - 125/2)/2 +kStatusBarHeight + 125/2);
  
  UIView *centerView = [[UIView alloc]initWithFrame:CGRectMake((self.deviceWidth -343)/2, 170, 343, 343)];
  centerView.backgroundColor = [UIColor clearColor];
  [self.view addSubview:centerView];
  
  centerView.center = CGPointMake(self.view.center.x, (self.deviceHeight - kStatusBarHeight -kTabBarHeight - 125/2)/2+kStatusBarHeight + 125/2);
  
  {
  UIView *recommendView = [[UIView alloc]initWithFrame:CGRectMake(150, 0, 75, 95)];
  UIButton *imageBt = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 75, 75)];
  [imageBt setImage:ImageWithContentsOfFileWithPng(@"index_recommend_icon@2x") forState:UIControlStateNormal];
  [imageBt addTarget:self action:@selector(clickRecommend:) forControlEvents:UIControlEventTouchUpInside];
  [recommendView addSubview:imageBt];
  UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0,75,  20, 20)];
  label.textColor = [UIColor whiteColor];
  label.text = @"金牛推荐";
  label.font = [UIFont systemFontOfSize:15];
  [recommendView addSubview:label];
  [label sizeToFit];
  label.center = CGPointMake(75/2, label.center.y);
  [centerView addSubview:recommendView];
  recommendView.center = CGPointMake(343/2, 45);
  
  }
  
  
  {
    UIView *uiView = [[UIView alloc]initWithFrame:CGRectMake(150, 150, 75, 95)];
    UIButton *imageView = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 75, 75)];
    [imageView setImage:ImageWithContentsOfFileWithPng(@"index_wealth_icon@2x") forState:UIControlStateNormal];
    [uiView addSubview:imageView];
    [imageView addTarget:self action:@selector(clickStar:) forControlEvents:UIControlEventTouchUpInside];
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0,75,  20, 20)];
    label.textColor = [UIColor whiteColor];
    label.text = @"星象财富";
    label.font = [UIFont systemFontOfSize:15];
    [uiView addSubview:label];
    [label sizeToFit];
    label.center = CGPointMake(75/2, label.center.y);
    [centerView addSubview:uiView];
    uiView.frame  = CGRectMake(3, 100, 75, 95);
    
  }
  
  
  {
    UIView *recommendView = [[UIView alloc]initWithFrame:CGRectMake(150, 150, 75, 95)];
    UIButton *imageView = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 75, 75)];
    [imageView setImage:ImageWithContentsOfFileWithPng(@"index_exchange_icon@2x")  forState:UIControlStateNormal];
    [recommendView addSubview:imageView];
    [imageView addTarget:self action:@selector(clickExchange:) forControlEvents:UIControlEventTouchUpInside];
    [recommendView addSubview:imageView];
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0,75,  20, 20)];
    label.textColor = [UIColor whiteColor];
    label.text = @"星转斗移";
    label.font = [UIFont systemFontOfSize:15];
    [recommendView addSubview:label];
    [label sizeToFit];
    label.center = CGPointMake(75/2, label.center.y);
    [centerView addSubview:recommendView];
    recommendView.frame  = CGRectMake(343-75-3, 100, 75, 95);
    
  }
  
  
  {
    UIView *recommendView = [[UIView alloc]initWithFrame:CGRectMake(150, 150, 75, 95)];
    UIButton *imageView = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 75, 75)];
    [imageView setImage:ImageWithContentsOfFileWithPng(@"index_moon_icon@2x") forState:UIControlStateNormal];
    [recommendView addSubview:imageView];
    [imageView addTarget:self action:@selector(clickMoon:) forControlEvents:UIControlEventTouchUpInside];
    
    [recommendView addSubview:imageView];
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0,75,  20, 20)];
    label.textColor = [UIColor whiteColor];
    label.text = @"众星捧月";
    label.font = [UIFont systemFontOfSize:15];
    [recommendView addSubview:label];
    [label sizeToFit];
    label.center = CGPointMake(75/2, label.center.y);
    [centerView addSubview:recommendView];
    recommendView.frame  = CGRectMake(70, 226, 75, 95);
    
  }
  
  {
    UIView *recommendView = [[UIView alloc]initWithFrame:CGRectMake(150, 150, 75, 95)];
    UIButton *imageView = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 75, 75)];
    [imageView setImage:ImageWithContentsOfFileWithPng(@"index_activity_icon@2x") forState:UIControlStateNormal];
    [recommendView addSubview:imageView];
    [imageView addTarget:self action:@selector(clickActivity:) forControlEvents:UIControlEventTouchUpInside];
    [recommendView addSubview:imageView];
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0,75,  20, 20)];
    label.textColor = [UIColor whiteColor];
    label.text = @"最新活动";
    label.font = [UIFont systemFontOfSize:15];
    [recommendView addSubview:label];
    [label sizeToFit];
    label.center = CGPointMake(75/2, label.center.y);
    [centerView addSubview:recommendView];
    recommendView.frame  = CGRectMake(343-75-70, 226, 75, 95);
    
  }
  

}


-(void)clickRecommend:(UIButton *)sender{
  
  [self gotoHtml:@"金牛推荐"];
}

-(void)loginClick{
  
  LoginViewController *target = [[LoginViewController alloc]initWithNibName:@"LoginViewController" bundle:nil];
  [[HomeViewController sharedHomeViewController].navigationController pushViewController:target animated:YES];

}

-(void)registerClick{
  
  RegisterViewController *target = [[RegisterViewController alloc]initWithNibName:@"RegisterViewController" bundle:nil];
  [[HomeViewController sharedHomeViewController].navigationController pushViewController:target animated:YES];
  
}



-(void)clickStar:(UIButton *)sender{
  
  [self gotoHtml:@"星象财富"];
}

-(void)gotoHtml:(NSString *)key{
  [self showSimplePro];
  @weakify(self);
  [[Api shareApiInstance]gotoHtmlWith:key AndResponce:^(id object) {
    @strongify(self);
    
    [self hideSimpleProWithDelay:0];
    
    NSString *title = [NSString stringWithFormat:@"%@",[object objectForKey:@"key"]];
    NSString *url = [NSString stringWithFormat:@"%@",[object objectForKey:@"url"]];
   
    WebViewController *webVC = [[WebViewController alloc]init];
    webVC.url = url;
    webVC.title = title;
    [[HomeViewController sharedHomeViewController].navigationController pushViewController:webVC animated:YES];
    
  } AndError:^(id object, NSError *error) {
    @strongify(self);
    [self hideSimpleProWithDelay:0];
    
    if(object!=nil){
      [self showPop:[NSString stringWithFormat:@"%@",[object objectForKey:@"msg"]]];
    }
      
  }];
}

-(void)clickExchange:(UIButton *)sender{
  [self gotoHtml:@"星转斗移"];
}

-(void)clickMoon:(UIButton *)sender{
  [self gotoHtml:@"众星捧月"];
  
}

-(void)clickActivity:(UIButton *)sender{
  [self gotoHtml:@"最新活动"];
}


-(void)viewWillDisappear:(BOOL)animated{
  //  [super viewDidAppear:animated];
  self.tabBarController.tabBarController.tabBar.hidden = NO;
}

-(void)viewWillAppear:(BOOL)animated{
  //  [super viewDidAppear:animated];
  self.tabBarController.tabBarController.tabBar.hidden = NO;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)dealloc{
  [[NSNotificationCenter defaultCenter]removeObserver:self];
}



@end
